package com.itheima.reggie.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    //新增套餐接口
    @PostMapping
    @Transactional
    public R saveSetmeal(@RequestBody SetmealDto setmealDto){
        //保存套餐数据到 setmeal表中
        setmealService.save(setmealDto);

        //保存套餐菜品数据到 setmeal_dish表中
        //获取当前套餐的菜品列表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //循环遍历，获取每一个套餐菜品数据 setmealDish
        //获取套餐id，并set到setmealDish中
        Long id = setmealDto.getId();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        //保存套餐菜品数据 setmealDish
        setmealDishService.saveBatch(setmealDishes);

        return R.success("保存套餐成功！！");
    }

    //套餐分页查询
    @GetMapping("/page")
    public R setmealPage(Integer page,Integer pageSize,String name){

        IPage<Setmeal> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        qw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,qw);

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> setmealDtos=new ArrayList<>();
        for (Setmeal setmeal : records) {
            SetmealDto setmealDto = new SetmealDto();

            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();

            setmealDto.setCategoryName(categoryName);
            BeanUtil.copyProperties(setmeal,setmealDto);

            setmealDtos.add(setmealDto);
        }

        Integer total = Math.toIntExact(pageInfo.getTotal());
        Map data=new HashMap();
        data.put("records",setmealDtos);
        data.put("total",total);

        return R.success(data);
    }

    @DeleteMapping
    public R deleteSetmeal(@RequestParam List<Long> ids){

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1);
        int count = setmealService.count(qw);

        if (count==ids.size()){
            LambdaQueryWrapper<SetmealDish> qw1 = new LambdaQueryWrapper<>();
            qw1.in(SetmealDish::getSetmealId,ids);
            setmealDishService.remove(qw1);

            setmealService.removeByIds(ids);

            return R.success("套餐删除成功");

        }
        return R.error("抱歉无法删除,您选中的套餐中含有处于售卖状态的套餐");

    }

    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        List<Setmeal> setmeals = setmealService.listByIds(ids);
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(status);
        }
        setmealService.updateBatchById(setmeals);
        return R.success("操作成功");
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        SetmealDto setmealDto = new SetmealDto();

        Setmeal setmeal = setmealService.getById(id);
        BeanUtil.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> qw=new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        qw.orderByDesc(SetmealDish::getUpdateTime);
        List<SetmealDish> list = setmealDishService.list(qw);
        setmealDto.setSetmealDishes(list);

        return R.success(setmealDto);
    }

    @PutMapping
    @Transactional
    public R updatesetmeal(@RequestBody SetmealDto setmealDto){

        setmealService.updateById(setmealDto);

        Long id = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        setmealDishService.remove(qw);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishService.saveBatch(setmealDishes);

        return R.success("修改套餐成功");


    }

    @GetMapping("/list")
    public R getlist(Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(qw);

        return R.success(list);
    }





















}
